/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.screen;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.screen.WeddingScreen;
import com.boxin.ims.modules.wechat.service.screen.WeddingScreenService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 婚庆微信墙Controller
 * @author Jakemanse
 * @version 2013-11-01
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/screen/weddingScreen")
public class WeddingScreenController extends BaseController {

	@Autowired
	private WeddingScreenService weddingScreenService;
	
	@ModelAttribute
	public WeddingScreen get(@RequestParam(required=false) Long id) {
		if (id != null){
			return weddingScreenService.get(id);
		}else{
			return new WeddingScreen();
		}
	}
	
	@RequiresPermissions("wechat:screen:weddingScreen:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeddingScreen weddingScreen, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			weddingScreen.setUser(user);
		}
        Page<WeddingScreen> page = weddingScreenService.find(new Page<WeddingScreen>(request, response), weddingScreen); 
        model.addAttribute("page", page);
		return "modules/wechat/screen/weddingScreenList";
	}

	@RequiresPermissions("wechat:screen:weddingScreen:view")
	@RequestMapping(value = "form")
	public String form(WeddingScreen weddingScreen, Model model) {
		User user = UserUtils.getUser();
		if(weddingScreen.getUser()==null){
			weddingScreen.setUser(user);
		}
		model.addAttribute("weddingScreen", weddingScreen);
		return "modules/wechat/screen/weddingScreenForm";
	}

	@RequiresPermissions("wechat:screen:weddingScreen:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("weddingPicFile") MultipartFile weddingPicFile,WeddingScreen weddingScreen, HttpServletRequest request, HttpServletResponse response,Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, weddingScreen)){
			return form(weddingScreen, model);
		}
		
		//秒杀页面的图片
		if(weddingPicFile != null && weddingPicFile.getOriginalFilename().length()>4){
			String weddingPath = WechatUtils.getWechatWeddingScreenResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(weddingPath);
			String sname = weddingPicFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_wedding_"+System.currentTimeMillis()+fileType;
			File file = new File(weddingPath+fname);
			System.out.println("保存文件:"+weddingPath+fname);
			try {
				weddingPicFile.transferTo(file);
				weddingScreen.setPicName(fname);
			} catch (Exception e) {
				System.out.println("上传文件失败:"+weddingPath+fname);
				e.printStackTrace();
			}
			
		}
		
				
		
		weddingScreenService.save(weddingScreen);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/screen/weddingScreen/?repage";
	}
	
	@RequiresPermissions("wechat:screen:weddingScreen:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		weddingScreenService.delete(id);
		addMessage(redirectAttributes, "删除婚庆微信墙成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/screen/weddingScreen/?repage";
	}

}
